package com.laiketui.plugin.common.mapper;

import com.laiketui.plugin.common.domain.base.seckill.SecondsProModel;
import com.laiketui.plugin.common.tool.db.BaseMapper;
import com.laiketui.core.exception.LaiKeAPIException;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


/**
 * 秒杀配置表
 *
 * @author Trick
 * @date 2020/10/14 11:58
 */
public interface SecondsProModelMapper extends BaseMapper<SecondsProModel> {


    /**
     * 获取当前进行中的秒杀商品
     *
     * @param map -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/10/14 12:00
     */
    List<Map<String, Object>> getCurrentSeconds(Map<String, String> map) throws LaiKeAPIException;


    /**
     * 获取秒杀商品列表
     * 【请使用 getSecGoodsList】
     *
     * @param map -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/8 15:38
     */
    @Deprecated
    List<Map<String, Object>> secGoodsList(Map<String, Object> map) throws LaiKeAPIException;

    @Deprecated
    int countSecGoodsList(Map<String, Object> map) throws LaiKeAPIException;


    /**
     * 订单确认页面获取商品信息（结算）
     *
     * @param storeId -
     * @param needNum -
     * @param secId   -
     * @param attrId   - 规格id 新秒杀是商品,未到规格
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/9 16:49
     */
    @Select("select m.product_title,m.volume,a.seconds_price price,a.price_type,c.price attrPrice,c.unit,c.attribute,c.img,c.yprice,m.freight" +
            " ,m.product_class,m.brand_id,m.weight,m.mch_id,m.is_distribution  " +
            " from lkt_seconds_activity a inner join lkt_product_list AS m on a.goodsId=m.id LEFT JOIN lkt_configure AS c ON c.id=#{attrId} " +
            " where m.store_id = #{storeId} and a.num >= #{needNum} and m.status = 2 and a.id=#{secId} ")
    Map<String, Object> settlementProductsInfo(int storeId, int needNum, int secId, int attrId) throws LaiKeAPIException;


    /**
     * 获取秒杀后台活动列表
     *
     * @param storeId -
     * @return List
     * @author Trick
     * @date 2021/5/6 10:06
     */
    @Select("SELECT sp.*,pl.product_title,c.price,m.name " +
            "FROM `lkt_seconds_pro` as sp " +
            "LEFT JOIN lkt_product_list as pl on pl.id = sp.pro_id " +
            "LEFT JOIN lkt_configure as c on c.id = sp.attr_id " +
            "LEFT JOIN lkt_mch as m on m.id = pl.mch_id " +
            "WHERE sp.store_id = #{storeId} and sp.is_delete = 0")
    List<Map<String, Object>> getAdminSecondsList(int storeId);


    /**
     * 获取秒杀预设库存数量
     *
     * @param attrid -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/5/7 13:50
     */
    @Select("SELECT if(sum(a.num) is null,0,sum(a.num)) FROM( " +
            " SELECT CASE resetting WHEN 1 THEN IF ( datediff(endtime, starttime) = 0, '1', datediff(endtime, starttime) + 1) * num ELSE num END AS num," +
            " pro_id FROM lkt_seconds_pro WHERE is_delete = 0 AND CURRENT_TIMESTAMP BETWEEN starttime AND ADDTIME(endtime, '23:59:59') " +
            " AND attr_id = #{attrid} ) a")
    int selectStockNum(int attrid) throws LaiKeAPIException;

    /**
     * 增减库存
     *
     * @param secId -
     * @param num   -
     * @return int
     * @author Trick
     * @date 2021/4/12 15:09
     */
    @Update("update lkt_seconds_pro set num = num + #{num},max_num=max_num+#{num} where id=#{secId} and is_delete  = 0")
    int addStockNum(int secId, int num);


    @Update("update lkt_seconds_pro set num = num + #{num} where id=#{secId} and is_delete  = 0")
    int addStockNum1(int secId, int num);

    //获取标签下的秒杀商品
    List<Map<String, Object>> getSecondsGoodsListByLabelId(Map<String, Object> map) throws LaiKeAPIException;

    int countSecondsGoodsListByLabelId(Map<String, Object> map) throws LaiKeAPIException;


    //获取用户秒杀购买数量
    @Select(" select if(b.num is null,0,sum(b.num)) from lkt_seconds_record a,lkt_order b where a.sno=b.sno " +
            " and a.activity_id=#{activityId} and a.user_id=#{userId} and b.recycle = 0 " +
            " and b.status !=7 ")
    int getUserSecNum(int activityId,String userId);

    /**
     * 商品规格删除，秒杀插件预扣库存同样进行回收规格的库存回收
     * @param attrIds
     * @return
     */
    @Update("<script>" +
            "update lkt_seconds_pro p, lkt_seconds_activity a set p.is_delete = 1,a.num = a.num - p.num, a.max_num = a.max_num - p.max_num where " +
            "p.activity_id = a.id " +
            " <if test='attrIds != null '> " +
            "   <foreach collection=\"attrIds\" item=\"id\" separator=\",\" open=\"and p.attr_id in(\" close=\")\"> " +
            "        #{id,jdbcType=INTEGER}" +
            "   </foreach> " +
            "</if> " +
            "</script>")
    int deleteByAttrId(@Param("attrIds") List<Integer> attrIds);
}